﻿using System.Xml.Serialization;
using Engine.Math;
using Microsoft.Xna.Framework;
using ThinkDotNetBotApiCommon.Ammos;
using ThinkDotNetEngine.Bots.Weapons;

namespace ThinkDotNetEngine.Bots.Ammos
{
    [XmlInclude(typeof(PlasmaAmmo))]
    [XmlInclude(typeof(RocketAmmo))]
    [XmlInclude(typeof(TacticalMissileAmmo))]
    public class AmmoBase:IAmmoBase
    {
        public  AmmoBase()
        {
            Speed = 1;
            MaxDuration = 5000;
        }
        public  AmmoBase(Game game,AmmoBase ammo)
        {
            MaxDuration = ammo.MaxDuration;
            HitPower = ammo.HitPower;
            Speed = ammo.Speed;
        }

        [XmlIgnore]
        public WeaponBase Weapon { get; set; }
        [XmlIgnore]
        public Vector3 CurrentPosition { get;  set; }
        [XmlIgnore]
        public Vector3 CurrentDirection { get; set; }
        public long MaxDuration { get; set; }
        [XmlIgnore]
        public long CurrentDuration { get; private set; }
        public float HitPower { get; set; }
        public float Speed { get; set; }


        public Vector2 ComputeObjectRotation()
        {
            float y = 0;
            if(CurrentDirection.X!=0&&CurrentDirection.Z!=0)
                y=(float)MathHelperEx.GetAngleBetweenVectors(Vector2.UnitY, new Vector2(CurrentDirection.X, CurrentDirection.Z));

            float x = (float)MathHelperEx.GetAngleBetweenVectors(Vector2.UnitY, new Vector2(1+CurrentDirection.Y, CurrentDirection.Y));

            return new Vector2(x, y);
        }
        public void Update(GameTime gameTime)
        {
            if (CurrentDuration < MaxDuration)
            {
                CurrentDuration += gameTime.ElapsedGameTime.Milliseconds;
                OnUpdate(gameTime);
            }
        }
        virtual protected void OnUpdate(GameTime gameTime)
        {
            CurrentPosition += Speed* CurrentDirection;
        }
    }
}
